package com.xiaoxx.controller.chat;

import com.xiaoxx.annotation.LogRecords;
import com.xiaoxx.common.error.ErrorException;
import com.xiaoxx.common.result.R;
import com.xiaoxx.common.result.RType;
import com.xiaoxx.model.dto.chat.ChatMenuDto;
import com.xiaoxx.model.dto.chat.ChatMenuQueryDto;
import com.xiaoxx.model.vo.chat.ChatMenListVo;
import com.xiaoxx.service.ChatMenuService;
import com.xiaoxx.utils.JwtUtil;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @author :HX
 * @date : 2023/7/19
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/api/chat/menu")
@Api(value = "ChatMenuController", tags = "聊天菜单管理")
public class ChatMenuController {
    @Resource
    private ChatMenuService chatMenuService;
    @Resource
    private HttpServletRequest request;

    @ApiOperation("新增用户聊天菜单")
    @ApiImplicitParams({@ApiImplicitParam(name = "chatMenuDto", value = "聊天菜单", required = true, dataType = "ChatMenuDto", paramType = "Body")})
    @ApiResponses(value = {@ApiResponse(code = 200, message = "响应成功")})
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @LogRecords(moduleName = "聊天菜单管理", content = "新增用户聊天菜单")
    public R<String> add(@Valid @RequestBody ChatMenuDto chatMenuDto) {
        chatMenuDto.setUserId(JwtUtil.getJwtUser(request).getUserId());
        return R.success(chatMenuService.addChatMenu(chatMenuDto));
    }

    @ApiOperation("修改用户聊天菜单")
    @ApiImplicitParams({@ApiImplicitParam(name = "chatMenuDto", value = "聊天菜单", required = true, dataType = "ChatMenuDto", paramType = "Body")})
    @ApiResponses(value = {@ApiResponse(code = 200, message = "响应成功")})
    @RequestMapping(value = "/upd", method = RequestMethod.POST)
    @LogRecords(moduleName = "聊天菜单管理", content = "修改聊天菜单")
    public R<Boolean> upd(@Valid @RequestBody ChatMenuDto chatMenuDto) {
        if (!StringUtils.hasLength(chatMenuDto.getId())) {
            throw new ErrorException(RType.PARAM_ID_REQUIRED_TRUE);
        }
        chatMenuDto.setUserId(JwtUtil.getJwtUser(request).getUserId());
        return R.success(chatMenuService.updChatMenu(chatMenuDto));
    }

    @PostMapping("/list")
    @ApiOperation("用户聊天菜单列表")
    @ApiResponses(value = {@ApiResponse(response = ChatMenListVo.class, code = 200, message = "响应成功")})
    public R<List<ChatMenListVo>> list(@RequestBody ChatMenuQueryDto chatMenuQueryDto) {
        chatMenuQueryDto.setUserId(JwtUtil.getJwtUser(request).getUserId());
        return R.success(chatMenuService.getListByUserId(chatMenuQueryDto));
    }

    @GetMapping("/del")
    @ApiOperation("删除聊天菜单")
    @LogRecords(moduleName = "聊天菜单管理", content = "删除聊天菜单")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "聊天菜单id", required = true, dataType = "String", paramType = "query")})
    @ApiResponses(value = {@ApiResponse(response = Boolean.class, code = 200, message = "响应成功")})
    public R<Boolean> del(@RequestParam String id) {
        return R.success(chatMenuService.removeById(id));
    }

}
